<?php

namespace Sbm;

class MasterSearch extends ObjectSearch
{

	function GetDefaultFilters()
	{
		return array(
			'select' => "`{master}`.*",
			'from' => "`{master}`",
		);
	}

	/**
	 * Возвращает список Master по массиву $profession_ids
	 * В результате возвращается массив array(profession_id1=>array(Master1,Master2,...), profession_id2=>array(Master2,...),...)
	 *   
	 * @param $master_ids
	 * @return array of array
	 */
	function GetByProfessions( $profession_ids )
	{
		foreach ($profession_ids as $v)
		{
			$result[$v] = array();
		}
		$s = "SELECT 
		             `ink`.`profession_id`,
		             `master`.* 
		      FROM 
		             `{master_profession}` as `ink` LEFT JOIN
		             `{master}` as `master` ON `ink`.`master_id` = `master`.`id`
		      WHERE 
		             `ink`.`profession_id` IN ('" . implode("','", $profession_ids) . "')";
		$q = $this->db->Query($s);
		$z = array();
		while ($r = $q->FetchRow())
		{
			$mid = $r['id'];
			$profession_id = $r['profession_id'];
			if ( !isset($z[$mid]) )
			{
				$z[$mid] = new Master($r);
			}
			$result[$profession_id][] = &$z[$mid];
		}
		return $result;
	}

	function CreateOneItem( $r )
	{
		$master = new Master($r);
		return $master;
	}

}